package com.tm.controller;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.tm.CoreConstants;
import com.tm.model.EcMemberInfo;
import com.tm.service.CommonService;
import com.tm.service.EcMemberInfoService;
import com.tm.service.OrderService;

@Controller
@RequestMapping(value = "/orderView")
public class EcOrderViewController {

	private Logger logger = Logger.getLogger(EcOrderViewController.class);
	
	@Autowired
	private OrderService oService;
	
	@Autowired
	private EcMemberInfoService emiService;
	
	@Autowired
	private CommonService cService;
	
	/**
	 * 跳转至个人订单列表页面
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(ModelMap model, HttpServletRequest request, HttpServletResponse response) {
		System.out.println("-------------------------------跳转至个人订单列表页面begin-----------------------------------------");
		try {
			
			if(request.getSession().getAttribute("memberId") == null){
				response.sendRedirect(CoreConstants.SJ_DOMAIN);
			}

			Integer memberId = (Integer) request.getSession().getAttribute("memberId");
			
			//获取订单取消原因
			List<Map<String, Object>> reasonList = oService.getOrderCancelReason();
			
			model.addAttribute("reasonList", reasonList);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("-------------------------------跳转至个人订单列表页面end-----------------------------------------");

		return "member/order/list";
		
	}
	
	/**
	 * 跳转至个人提现页面
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/withdraw", method = RequestMethod.GET)
	public String withdraw(ModelMap model, HttpServletRequest request, HttpServletResponse response) {
		System.out.println("-------------------------------跳转至个人提现页面begin-----------------------------------------");
		try {
			
			if(request.getSession().getAttribute("memberId") == null){
				response.sendRedirect(CoreConstants.SJ_DOMAIN);
			}

			Integer memberId = (Integer) request.getSession().getAttribute("memberId");
			
			EcMemberInfo member = emiService.getmemberByMemberId(memberId);
			
			model.addAttribute("member", member);
			
			//获取提现手续费
			List<Map<String, Object>> list = cService.getSysCodeByTypeNo("tx_rate");
			
			BigDecimal txRate = new BigDecimal(0);
			if(list.size() > 0){
				txRate = new BigDecimal(String.valueOf(list.get(0).get("code_value")));
			}
			
			model.addAttribute("txRate", txRate);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("-------------------------------跳转至个人提现页面end-----------------------------------------");

		return "member/order/withdraw";
		
	}
	
	/**
	 * 跳转至门店销售订单列表页面
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/saleOrderList", method = RequestMethod.GET)
	public String saleOrderList(ModelMap model, HttpServletRequest request, HttpServletResponse response) {
		System.out.println("-------------------------------跳转至门店销售订单列表页面begin-----------------------------------------");
		try {
			
			if(request.getSession().getAttribute("memberId") == null){
				response.sendRedirect(CoreConstants.SJ_DOMAIN);
			}

			Integer memberId = (Integer) request.getSession().getAttribute("memberId");
			
			//获取订单取消原因
			/*List<Map<String, Object>> reasonList = oService.getOrderCancelReason();
			
			model.addAttribute("reasonList", reasonList);*/
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("-------------------------------跳转至门店销售订单列表页面end-----------------------------------------");

		return "member/order/sale-order-list";
		
	}
	
	/**
	 * 跳转至订单发货页面
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/orderConfirm", method = RequestMethod.GET)
	public String orderConfirm(ModelMap model, HttpServletRequest request, HttpServletResponse response) {
		System.out.println("-------------------------------跳转至订单发货表页面begin-----------------------------------------");
		try {
			
			if(request.getSession().getAttribute("memberId") == null){
				response.sendRedirect(CoreConstants.SJ_DOMAIN);
			}

			Integer memberId = (Integer) request.getSession().getAttribute("memberId");
			String orderId = request.getParameter("orderId");
			
			//获取订单详情
			Map<String, Object> order = oService.getOrderDetailByOrderId(orderId);
			
			model.addAttribute("order", order);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("-------------------------------跳转至订单发货页面end-----------------------------------------");

		return "member/order/order-confirm";
		
	}
	
	/**
	 * 跳转至订单详情页面
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/detail", method = RequestMethod.GET)
	public String detail(ModelMap model, HttpServletRequest request, HttpServletResponse response) {
		System.out.println("-------------------------------跳转至订单详情页面begin-----------------------------------------");
		try {
			
			if(request.getSession().getAttribute("memberId") == null){
				response.sendRedirect(CoreConstants.SJ_DOMAIN);
			}

			Integer memberId = (Integer) request.getSession().getAttribute("memberId");
			String orderId = request.getParameter("orderId");
			
			//获取订单详情
			Map<String, Object> order = oService.getOrderDetailByOrderId(orderId);
			
			model.addAttribute("order", order);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("-------------------------------跳转至订单详情页面end-----------------------------------------");

		return "member/order/detail";
		
	}
	
	/**
	 * 跳转至订单退款页面
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/orderRefund", method = RequestMethod.GET)
	public String orderRefund(ModelMap model, HttpServletRequest request, HttpServletResponse response) {
		System.out.println("-------------------------------跳转至订单退款页面begin-----------------------------------------");
		try {
			
			if(request.getSession().getAttribute("memberId") == null){
				response.sendRedirect(CoreConstants.SJ_DOMAIN);
			}

			Integer memberId = (Integer) request.getSession().getAttribute("memberId");
			String orderId = request.getParameter("orderId");
			
			//获取订单详情
			Map<String, Object> order = oService.getOrderDetailByOrderId(orderId);
			
			model.addAttribute("order", order);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("-------------------------------跳转至订单退款页面end-----------------------------------------");

		return "member/order/order-refund";
		
	}
	
}
